package org.jeecg.chase.common.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author leikai
 * @version 1.0
 * @description: map和javaBean转换工具
 * @date 2021/07/23/0023 16:38
 */
public class BeanUtils {

    private static final String[] EMPTY_ARRAY = new String[0];

    private static final List EMPTY_LIST = new ArrayList(0);

    private static ObjectMapper mapper;

    static {
        mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.addHandler(new ProbleHandler());
    }

    /**
     * JavaBean转换为map
     *
     * @param bean
     * @return java.util.Map
     * @author leikai
     * @date 2021/07/26/0026 16:51
     */
    public static Map bean2Map(Object bean) {
        try {
            String json = mapper.writeValueAsString(bean);
            Map map = (Map) JSON.parse(json);
            return map;
        } catch (JsonProcessingException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
            return null;
        }
    }

    /**
     * map转换为JavaBean
     *
     * @param map
     * @param clazz
     * @return java.lang.Object
     * @author leikai
     * @date 2021/07/26/0026 16:51
     */
    public static Object map2Bean(Map<String, Object> map, Class<?> clazz) {
        Object object = null;
        try {
            String json = mapper.writeValueAsString(map);
            object = mapper.readValue(json, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }


    static class ProbleHandler extends DeserializationProblemHandler {
        @Override
        public Object handleUnexpectedToken(DeserializationContext ctxt,
                                            Class<?> targetType, JsonToken t, JsonParser p,
                                            String failureMsg)
                throws IOException {
            if (t == JsonToken.VALUE_STRING && String[].class.isAssignableFrom(targetType)) {
                String value = p.getValueAsString();
                if (StringUtils.isNotBlank(value)) {
                    return value.split(",");
                }
                return EMPTY_ARRAY;
            } else if (t == JsonToken.VALUE_STRING && List.class.isAssignableFrom(targetType)) {
                String value = p.getValueAsString();
                if (StringUtils.isNotBlank(value)) {
                    String[] v = value.split(",");
                    List<String> vv = new ArrayList<>(v.length);
                    for (String s : v) {
                        vv.add(s);
                    }
                    return vv;
                }
                return EMPTY_LIST;
            }
            return null;
        }
    }
}
